package com.lijianlove.log.loghelper.controller;

import com.lijianlove.log.loghelper.request.UpdateLogLevelRequest;
import com.lijianlove.log.loghelper.utils.ChangeLogLevelProcessUnit;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.slf4j.impl.StaticLoggerBinder;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author lijian
 * @date 2019/5/7 下午5:05
 */
@Slf4j
@SpringBootApplication
@RestController
public class LogController {

    /**
     * 修改日志级别
     *
     * @param request
     * @return
     */
    @RequestMapping("/syslog/update")
    public Map<String, Object> update(@RequestBody UpdateLogLevelRequest request) {

        String type = StaticLoggerBinder.getSingleton().getLoggerFactoryClassStr();
        ch.qos.logback.classic.LoggerContext loggerContext = (ch.qos.logback.classic.LoggerContext) LoggerFactory.getILoggerFactory();

        log.info("info ...");
        log.debug("debug ...");
        log.warn("warn ...");
        log.error("error ...");

        Map<String, Object> loggerList = ChangeLogLevelProcessUnit.getLoggerList();
        for (String key : loggerList.keySet()) {
            log.info(" key {} , value {}", key, loggerList.get(key));
        }
        Map<String, Object> newLoggerList = ChangeLogLevelProcessUnit.setLevel(request.getBeanName(), request.getLevel());


        log.info("info ...");
        log.debug("debug ...");
        log.warn("warn ...");
        log.error("error ...");

        return newLoggerList;
    }

    /**
     * 注意只可以将日志级别重置为上次的，而不是真的最开始那一次的
     *
     * @return
     */
    @RequestMapping("/syslog/reset")
    public Map<String, Object> reset() {
        return ChangeLogLevelProcessUnit.reset();
    }

    /**
     * 当前日志级别
     *
     * @return
     */
    @RequestMapping("/syslog/list")
    public Map<String, Object> list() {
        return ChangeLogLevelProcessUnit.getLoggerList();
    }


}
